package com.baili.healthauth.service.impl;

import com.baili.healthauth.service.AuthService;
import com.baili.healthcommoncore.domain.redis.RedisCache;
import com.baili.healthcommoncore.domain.result.Result;
import com.baili.healthsystem.domain.User;
import com.baili.healthsystem.domain.request.UserRequest;
import com.baili.healthsystemremote.remote.RemoteSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.IllegalFormatCodePointException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author：ShenJiaFeng
 * @Package：com.baili.healthauth.service.impl
 * @Project：baili-health-cloud
 * @name：AuthServiceImpl
 * @Date：2024/5/26 15:11
 */
@Service
public class AuthServiceImpl implements AuthService {
    //远程调用
    @Autowired
    private RemoteSystemService remoteSystemService;
    @Autowired
    private RedisCache redisCache;
    @Override
    public Result<User> sendCode(UserRequest userRequest) {
        //远程调用 根据手机号/邮箱 获取用户信息
        Result<User> byUserPhoneOrEmail = remoteSystemService.findByUserNameOrUserEmailOrPhoneOrIdCard(userRequest);
        User data = byUserPhoneOrEmail.getData();
        if (data == null){
            throw new IllegalArgumentException("用户不存在 !");
        }
        //手机号不为空
        if (userRequest.getPhone() != null &&!userRequest.getPhone().isEmpty()){
            int code = new Random().nextInt(9000)+1000;
            //根据手机号 存入redis  设置过期时间1分钟
            redisCache.setCacheObject(userRequest.getPhone(),code,1L, TimeUnit.MINUTES);
        }

        if (userRequest.getUserEmail() !=null && !userRequest.getUserEmail().isEmpty()){

        }


        return Result.success();
    }
}
